# FIGURE 2
# Authors: Mark Kayser & Kasia Nalewajko
# First created: Feb 6, 2023
# Replicated: Nov 18, 2025

rm(list = ls())

# LOAD PACKAGES -----------------------------------------------------------

if (!require("dplyr")) install.packages("dplyr")
if (!require("glmnet")) install.packages("glmnet")
if (!require("marginaleffects")) install.packages("marginaleffects")
if (!require("ggplot2")) install.packages("ggplot2")

# LOAD DATA ---------------------------------------------------------------

setwd("set/path/to/folder")

load(file = "./01 data/02 generated data/study2/02results_fullsample_clean.Rda")
results <- results %>% 
  filter(outcome != "99") %>% 
  filter(manipulation_check == 3)

# RUN MODELS AND PLOT ----

# LEFT PANE: PO supporters who support selling the port

# Logit model

# recode outcome
results <- results %>% 
  mutate(outcome_supports = ifelse(outcome > 5, 1, 0))

temp <- results %>% 
  filter(popis == "PO",
         !is.na(GSparty_id_z))

modelPO <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))


# Plot

plot_predictions(modelPO,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PO supporters, PO party wants to sell",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to sell the port") +
  theme_minimal() +
  scale_color_manual(values = c("ChinaPO" = "blue", "ChinaPiS" = "transparent", "China" = "transparent")) +
  scale_fill_manual(values = c("ChinaPO" = "blue", "ChinaPiS" = "transparent", "China" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.2, 1)


# RIGHT PANE: PiS supporters who support selling the port ----

temp <- results %>% 
  filter(popis == "PiS",
         !is.na(GSparty_id_z))

modelPiS <- glm(outcome_supports ~ exp_condition*GSparty_id_z + experiment_first + as.factor(voivodeship), data = temp, family = binomial(link=logit))

plot_predictions(modelPiS,
                 type = "response",
                 condition = c("GSparty_id_z", "exp_condition")) +
  labs(title = "PiS supporters, PiS party wants to sell",
       x = "Standardised strength of partisanship",
       y = "Predicted probability of wanting to sell the port") +
  theme_minimal()+
  scale_color_manual(values = c("ChinaPO" = "transparent", "ChinaPiS" = "red", "China" = "transparent")) +
  scale_fill_manual(values = c("ChinaPO" = "transparent", "ChinaPiS" = "red", "China" = "transparent")) +
  theme(legend.position = "none") +
  ylim(-0.2, 1)

